package com.yzf.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.yzf.entity.Contact;
import com.yzf.util.db.DBHelp;
import com.yzf.util.db.RowMapper;

public class ContactDao {
	
	private DBHelp<Contact> db = new DBHelp<Contact>();
	
	public void save(Contact contact){
		String sql = "INSERT INTO t_contact (id,`name`,companyname,moblie,tel,email,address,website,weibo,content,projectid,createtime) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
		System.out.println(contact.getName());
		db.executeSQL(sql,contact.getId(), contact.getName(),contact.getCompanyname(),contact.getMoblie(),contact.getTel(),contact.getEmail(),contact.getAddress(),contact.getWebsite(),contact.getWeibo(),contact.getContent(),contact.getProjectid(),contact.getCreatetime());
	}
	public void update(Contact contact){
		String sql = "update t_contact set `name`=?, companyname=?, moblie=? ,tel=? ,email=? ,address=?, website=? ,weibo=? ,content=?, projectid=?, createtime=? where id=?";
		db.executeSQL(sql, contact.getName(),contact.getCompanyname(),contact.getMoblie(),contact.getTel(),contact.getEmail(),contact.getAddress(),contact.getWebsite(),contact.getWeibo(),contact.getContent(),contact.getProjectid(),contact.getCreatetime(),contact.getId());
	}
	public List<Contact> findAll(){
		String sql = "select id,`name`,companyname,moblie,tel,email,address,website,weibo,content,projectid,createtime from t_contact order by createtime desc";
		return db.executeQueryToList(sql, new ContactRowMapper());
	}
	
	public void delete(String id){
		String sql = "delete from t_contact where id=?";
		db.executeSQL(sql, id);
	}
	public Contact findById(String id){
		String sql = "select id,`name`,companyname,moblie,tel,email,address,website,weibo,content,projectid,createtime from t_contact where id=? order by createtime desc";
		return db.executeQueryToObject(sql, new ContactRowMapper(), id);
		
	}
	
	public List<Contact> findByProjectId(String p_id){
		String sql = "select id,`name`,companyname,moblie,tel,email,address,website,weibo,content,projectid,createtime from t_contact where projectid=? order by createtime desc";
		return db.executeQueryToList(sql, new ContactRowMapper(), p_id);
		
	}
	
	
	public Contact findByName(String name){
		String sql = "select id,`name`,companyname,mobile,tel,email,address,websize,weibo,content,projectid,createtime from t_contact where name=? order by createtime desc";
		return db.executeQueryToObject(sql, new ContactRowMapper(), name);
		
	}
	private class ContactRowMapper implements RowMapper<Contact>{

		@Override
		public Contact mapRow(ResultSet rs) throws SQLException {
			Contact con = new Contact();
			
			con.setId(rs.getString("id"));
			con.setName(rs.getString("name"));
			con.setCompanyname(rs.getString("companyname"));
			con.setMoblie(rs.getString("moblie"));
			con.setTel(rs.getString("tel"));
			con.setEmail(rs.getString("email"));
			con.setAddress(rs.getString("address"));
			con.setWebsite(rs.getString("website"));
			con.setWeibo(rs.getString("weibo"));
			con.setContent(rs.getString("content"));
			con.setProjectid(rs.getString("projectid"));
			con.setCreatetime(rs.getString("createtime"));
			
			return con;
		}
		
		
	}
}
